/**
* 	File: howlsh.c
* 	Author: S.Wilson
*	Contribution : E.Wilson
* 	Date: 2010-02-26
* 	Purpose: shell for Howl
*
**/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAX_SIZE 128

char username[1000];
char password[1000];

char* tokenise(char *temp){
	char * pch;
  	pch = strtok (temp," ");
	pch = strtok(NULL," ");
	//strtok(NULL," ");
  	if (pch != NULL){
	  	return pch;
	}
}

void process(char *temp){

char formedCommand[1000];

bzero(formedCommand, sizeof(formedCommand));

strcat(formedCommand,"python howl.py --email ");
// all the processing goes here
	if(temp[0] == '-' && temp[1] == 'u'){
		// the user wants to enter a username	
		sprintf(username, "%s",tokenise(temp));
	} else if (temp[0] == '-' && temp[1] == 'p'){
		sprintf(password, "%s",tokenise(temp));
	} else if ((temp[0] == 'e' && temp[1] == 'x') && (temp[2] == 'i' && temp[3] == 't')){
		printf("Exiting...\n");
		exit(1);
	} else if (temp[0] == '#'){
		return;
	} else {
		strcat(formedCommand, username);
		strcat(formedCommand, " --password ");
		strcat(formedCommand, password);
		strcat(formedCommand, " --command \"");
		strcat(formedCommand, temp);
		strcat(formedCommand, "\"");
		system(formedCommand);
		
	}
}


int main(int argc, char *argv[], char *envp[])
{
	char c = '\0';
	char temp[MAX_SIZE];
	bzero(temp, sizeof(temp));
	printf("\n[howlsh ] ");
	while(c != EOF) {
		c = getchar();
		switch(c) {
			case '\n': /* parse and execute. */
				process(temp);
				// function goes here to execute
				bzero(temp, sizeof(temp));
				printf("[howlsh ] ");
				break;
			default: strncat(temp, &c,1);
				break;
		}

	}
	printf("\n");
	return 0;
}

